package com.zsc.shixun.controller;


import com.zsc.shixun.common.CommonResult;
import com.zsc.shixun.common.ResultUtil;
import com.zsc.shixun.model.comment.CommentCreateVO;
import com.zsc.shixun.service.ICommentService;
import com.zsc.shixun.utils.LoginMessageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 评论管理 前端控制器
 * </p>
 *
 * @author ZWYZY
 * @since 2020-06-06
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
    private static final Logger logger = LoggerFactory.getLogger(CommentController.class);
    @Autowired
    private LoginMessageUtils loginMessageUtils;
    @Autowired
    private ICommentService commentService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        request.setAttribute("CommentVO", commentService.getData(loginMessageUtils.getMessage().getLoginUser().getId()));
        return "admin/comment/index";
    }

    /**
     * 获取评论列表
     *
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    @Secured(value = "ROLE_comment_select")
    public CommonResult getData() {
        return ResultUtil.success(commentService.getData(loginMessageUtils.getMessage().getLoginUser().getId()));
    }

    /**
     * 删除评论
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    @Secured(value = "ROLE_comment_delete")
    public CommonResult delete(@Validated @RequestParam(required = false) Long id) {
        commentService.delete(id);
        return ResultUtil.success("评论删除成功哦，亲");
    }

    /**
     * 文章页---增加评论
     *
     * @param commentCreateVO
     * @return
     */
    @PostMapping("/insert")
    @Secured(value = "ROLE_comment_insert")
    @ResponseBody
    public CommonResult insert(@Validated @ModelAttribute CommentCreateVO commentCreateVO) {
        commentCreateVO.setAuthor(loginMessageUtils.getMessage().getLoginUser().getUsername());
        commentService.insert(commentCreateVO);
        return ResultUtil.success("评论成功");
    }

}
